<?php

session_start();

header('Access-Control-Allow-Origin:*');

date_default_timezone_set("PRC");

header("Content-Type: text/html;charset=utf8");

require_once '../common/in_mysql.php';
require_once '../db/admin.db.php';

$action = filter_input(INPUT_POST, "action") ? filter_input(INPUT_POST, "action") : filter_input(INPUT_GET, "action");

$res = array();
switch ($action) {
    case 'login':
        $res = login();
        break;
    case 'checkSession':
        $res = checkSession();
        break;
    case 'listAdmin':
        $res = listAdmin();
        break;
    case 'addAdmin':
        $res = addAdmin();
        break;
    case 'delAdmin':
        $res = delAdmin();
        break;
    case 'activeAdmin':
        $res = activeAdmin();
        break;
    case 'resetPwd':
        $res = resetPwd();
        break;
    case 'modifyPwd':
        $res = modifyPwd();
        break;
    case 'editUser':
        $res = editAdmin();
        break;
    default :
}
echo json_encode($res);

function login()
{
    $username = filter_input(INPUT_POST, "username");
    $password = filter_input(INPUT_POST, "password");

    $adminDb = new AdminDb();
    $adminArray = $adminDb->login($username, $password);

    if (count($adminArray) > 0) {
        $_SESSION['adminId'] = $adminArray[0]['id'];
        $_SESSION['adminName'] = $adminArray[0]['name'];
        $_SESSION['id'] = $adminArray[0]['id'];
        $_SESSION['name'] = $adminArray[0]['name'];
        $_SESSION['p'] = $adminArray[0]['p'];
        $_SESSION['power'] = $adminArray[0]['power'];
        $res['code'] = 0;
        $res['msg'] = "";
        $p = $adminArray[0]['p'];
        $array = explode(",", $p);
        for ($i = 0; $i < count($array); $i++) {
            $res['p'][$i + 1] = $array[$i];
        }
    } else {
        $res['code'] = 201;
        $res['msg'] = "用户名或密码错误";
    }

    return $res;
}

function checkSession()
{
    $sessionId = filter_input(INPUT_POST, "sessionId");

    if (isset($_SESSION['adminId'])) {
        $res['code'] = 0;
        $res['msg'] = "";
    } else {
        $res['code'] = 200;
        $res['msg'] = "Session已过期";
    }
    return $res;
}

function listAdmin()
{
    $adminDb = new AdminDb();
    $adminList = $adminDb->listAdmin();
    $res['code'] = 0;
    $res['msg'] = "";
    $res['data'] = $adminList;
    $res['count'] = count($adminList);
    return $res;
}

function addAdmin()
{
    $username = filter_input(INPUT_POST, "username");
    $password = filter_input(INPUT_POST, "password");
    $p = filter_input(INPUT_POST, "p");
    $nick = filter_input(INPUT_POST, "nick");
    $adminDb = new AdminDb();
    $addtime = time();
    $data = $adminDb->queryAdmin($username);
    if (!$data) {
        if ($adminDb->addAdmin($username, $password, $addtime, $p, $nick)) {
            $res['code'] = 0;
            $res['msg'] = "添加管理员成功";
        } else {
            $res['code'] = 203;
            $res['msg'] = "添加管理员失败";
        }
        return $res;

    } else {
        $res['code'] = 204;
        $res['msg'] = "管理员已存在";
        return $res;
    }

}

function delAdmin()
{
    $userId = filter_input(INPUT_POST, "userId");
    $userName = filter_input(INPUT_POST, "name");

    $name = $_SESSION['name'];

    $adminDb = new AdminDb();

    if ($name != $userName) {
        if ($adminDb->delAdmin($userId)) {
            $res['code'] = 0;
            $res['msg'] = "删除管理员成功";
        } else {
            $res['code'] = 203;
            $res['msg'] = "删除管理员失败";
        }
    }
    else{
        $res['code'] = 204;
        $res['msg'] = "不能将自己删除哦！";
    }

    return $res;
}

function activeAdmin()
{
    $active = filter_input(INPUT_POST, "active");
    $adminId = filter_input(INPUT_POST, "adminId");
    if ($active == "active") {
        $status = 1;
    } else if ($active == "deactive") {
        $status = 0;
    } else {
        $res['code'] = 204;
        $res['msg'] = "参数不正确";
        return $res;
    }
    $adminDb = new AdminDb();
    if ($adminDb->updateStatus($adminId, $status) !== false) {
        $res['code'] = 0;
        $res['msg'] = "";
    } else {
        $res['code'] = 203;
        if ($active == "active") {
            $res['msg'] = "启用管理员失败";
        } else {
            $res['msg'] = "禁用管理员失败";
        }
    }
    return $res;
}

function resetPwd()
{
    $adminId = filter_input(INPUT_POST, "adminId");
    $password = "e10adc3949ba59abbe56e057f20f883e"; //重置密码为123456 前面为md5后的字符串
    $adminDb = new AdminDb();
    if ($adminDb->resetPwd($adminId, $password) !== false) {
        $res['code'] = 0;
        $res['msg'] = "";
        $res['password'] = "123456";
    } else {
        $res['code'] = 204;

        $res['msg'] = "重置管理员密码失败";
    }
    return $res;
}

function modifyPwd()
{
    $adminId = filter_input(INPUT_POST, "adminId");
    $oldPwd = filter_input(INPUT_POST, "oldPwd");
    $newPwd = filter_input(INPUT_POST, "newPwd");

    $adminDb = new AdminDb();
    if ($adminDb->modifyPwd($adminId, $oldPwd, $newPwd) != false) {
        $res['code'] = 0;
        $res['msg'] = "";
    } else {
        $res['code'] = 205;
        $res['msg'] = "修改密码失败";
    }
    return $res;
}

function editAdmin()
{
    $username = filter_input(INPUT_POST, "username");
    $p = filter_input(INPUT_POST, "p");
    $adminId = filter_input(INPUT_POST, "adminId");
    $adminDb = new AdminDb();
    if ($adminDb->editAdmin($adminId, $username, $p)) {
        $res['code'] = 0;
        $res['msg'] = "修改成功";
    } else {
        $res['code'] = 205;
        $res['msg'] = "修改失败";
    }
    return $res;

}
